package com.fmatos.amazingwallpapers.service.image;

import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.widget.Toast;

import com.fmatos.amazingwallpapers.service.image.BitmapsService.LocalBinder;

public class BitmapsServiceClient  extends ContextWrapper {

	BitmapsService mService;
    boolean mBound = false;

	public BitmapsServiceClient(Context base) {
		super(base);
	}

	public void send() {
		Intent intent = new Intent(this, BitmapsService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        
        if (mBound) {
            // Call a method from the LocalService.
            // However, if this call were something that might hang, then this request should
            // occur in a separate thread to avoid slowing down the activity performance.
            int num = mService.getRandomNumber();
            Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
        }
		
	}
	
	/** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };

}